﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Algobox.JuicyLib.MarketData.Structure;
using Algobox.JuicyLib.SQL.LiquidView;
using Algobox.JuicyLib.Apps.EODData;

namespace LiquidTrading.LiquidViewSyncTool
{
    public class EODData
    {
        static EODData()
        {
            _eodDataClient = new EODDataClient(Properties.Settings.Default.EODDataUsername, Properties.Settings.Default.EODDataPassword);
        }

        static EODDataClient _eodDataClient;

        /// <summary>
        /// Import stock and options settlement prices from EOD data ftp files
        /// </summary>
        public static void DoEOD()
        {

            DateTime date;

            if (DateTime.Today.DayOfWeek == DayOfWeek.Sunday)
                date = DateTime.Today.Subtract(new TimeSpan(2, 0, 0, 0));
            if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)
                date = DateTime.Today.Subtract(new TimeSpan(3, 0, 0, 0));
            else
                date = DateTime.Today.Subtract(new TimeSpan(1, 0, 0, 0));

            Dictionary<string, OHLC> items;
            int retval;

            int retryCount = 0;
            string umtf;

            // LSE
            var lseSymbolMap = (from csvline in Properties.Settings.Default.EODSymbolMap_LSE.Split(',')
                                let csv = csvline.Split(':')
                                where csv.Length == 2
                                select new
                                {
                                    EOD = csv[0],
                                    UMTF = csv[1]
                                }).ToDictionary(a => a.EOD, b => b.UMTF);
            try
            {
                var stocks = Program.DBLinq.InstrumentsStocks.Where(a => a.MarketMIC == "XLON" && a.UMTF != null).ToDictionary(a => a.UMTF, a => a.CurrencyId.Value);

                List<string> gbpSymbols = new List<string>();
                Properties.Settings.Default.EODData_GBPSymbols.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach((a) => { gbpSymbols.Add(a.Trim()); });

                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetEndOfDay(EODDataClient.Exchanges.LSE, date, out items))
                    {
                        decimal multiplier = 1;
                        int currencyId;

                        foreach (KeyValuePair<string, OHLC> item in items)
                        {
                            if (!lseSymbolMap.TryGetValue(item.Key, out umtf))
                                umtf = item.Key;

                            umtf = umtf + "l";

                            if (stocks.TryGetValue(umtf, out currencyId))
                            {
                                if (currencyId == 1280 && !gbpSymbols.Contains(item.Key))
                                    multiplier = 0.01m;
                                else
                                    multiplier = 1;

                                LiquidViewHelper.TryImportPriceByUMTF(date, umtf, "XLON", item.Value.Open * multiplier, item.Value.High * multiplier, item.Value.Low * multiplier, item.Value.Close * multiplier, item.Value.Close * multiplier, item.Value.Volume, "LiquidViewSyncTool - EOD Data", out retval);

                                if (retval == 0)
                                    Console.WriteLine("Success " + umtf + "l " + item.Value.ToString());
                            }
                        }
                        retryCount = Program.MAX_RETRIES;
                    }
                    else
                    {
                        Console.WriteLine("Failed to load EOD data file for XLON attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load EOD data file for XLON attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for EOD data file for XLON\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for EOD data file for XLON");
            }

            retryCount = 0;


            // NYSE
            try
            {
                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetEndOfDay(EODDataClient.Exchanges.NYSE, date, out items))
                    {
                        foreach (KeyValuePair<string, OHLC> item in items)
                        {
                            LiquidViewHelper.TryImportPriceByUSSymbol(date, item.Key, "XNYS", item.Value.Open, item.Value.High, item.Value.Low, item.Value.Close, item.Value.Close, item.Value.Volume, "LiquidViewSyncTool - EOD Data", out retval);

                            if (retval == 0)
                                Console.WriteLine("Success Nyse {0} {1}", item.Key, item.Value);
                        }
                        retryCount = Program.MAX_RETRIES;
                    }
                    else
                    {
                        Console.WriteLine("Failed to load EOD data file for Nyse attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load EOD data file for Nyse attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for EOD data file for Nyse\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for EOD data file for Nyse");
            }

            retryCount = 0;

            // Nasdaq
            try
            {
                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetEndOfDay(EODDataClient.Exchanges.Nasdaq, date, out items))
                    {
                        foreach (KeyValuePair<string, OHLC> item in items)
                        {
                            LiquidViewHelper.TryImportPriceByUSSymbol(date, item.Key, "XNAS", item.Value.Open, item.Value.High, item.Value.Low, item.Value.Close, item.Value.Close, item.Value.Volume, "LiquidViewSyncTool - EOD Data", out retval);

                            if (retval == 0)
                                Console.WriteLine("Success Nasdaq {0} {1}", item.Key, item.Value);
                        }
                        retryCount = Program.MAX_RETRIES;
                    }
                    else
                    {
                        Console.WriteLine("Failed to load EOD data file for Nasdaq attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load EOD data file for Nasdaq attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for EOD data file for Nasdaq\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for EOD data file for Nasdaq");
            }

            retryCount = 0;

            // Arca
            try
            {

                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetEndOfDay(EODDataClient.Exchanges.Amex, date, out items))
                    {
                        foreach (KeyValuePair<string, OHLC> item in items)
                        {
                            LiquidViewHelper.TryImportPriceByUSSymbol(date, item.Key, "ARCX", item.Value.Open, item.Value.High, item.Value.Low, item.Value.Close, item.Value.Close, item.Value.Volume, "LiquidViewSyncTool - EOD Data", out retval);

                            if (retval == 0)
                                Console.WriteLine("Success ARCX {0} {1}", item.Key, item.Value);
                        }
                        retryCount = Program.MAX_RETRIES;
                    }
                    else
                    {
                        Console.WriteLine("Failed to load EOD data file for ARCX attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load EOD data file for ARCX attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for EOD data file for ARCX\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for EOD data file for ARCX");
            }

            retryCount = 0;

            // Milan
            try
            {
                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetEndOfDay(EODDataClient.Exchanges.Milan, date, out items))
                    {
                        foreach (KeyValuePair<string, OHLC> item in items)
                        {
                            LiquidViewHelper.TryImportPriceByUMTF(date, item.Key + "m", "XMIL", item.Value.Open, item.Value.High, item.Value.Low, item.Value.Close, item.Value.Close, item.Value.Volume, "LiquidViewSyncTool - EOD Data", out retval);

                            if (retval == 0)
                                Console.WriteLine("Success " + item.Key + "m " + item.Value.ToString());
                        }
                        retryCount = Program.MAX_RETRIES;
                    }
                    else
                    {
                        Console.WriteLine("Failed to load EOD data file for XMIL attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load EOD data file for XMIL attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for EOD data file for XMIL\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for EOD data file for XMIL");
            }

            retryCount = 0;

            // Toronto Stock Exchnge - XTSE
            try
            {
                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetEndOfDay(EODDataClient.Exchanges.XTSE, date, out items))
                    {
                        foreach (KeyValuePair<string, OHLC> item in items)
                        {
                            LiquidViewHelper.TryImportPriceByCanadaSymbol(date, item.Key, "XTSE", item.Value.Open, item.Value.High, item.Value.Low, item.Value.Close, item.Value.Close, item.Value.Volume, "LiquidViewSyncTool - EOD Data", out retval);

                            if (retval == 0)
                                Console.WriteLine("Success XTSE {0} {1}", item.Key, item.Value);
                        }
                        retryCount = Program.MAX_RETRIES;
                    }
                    else
                    {
                        Console.WriteLine("Failed to load EOD data file for XTSE attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load EOD data file for XTSE attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for EOD data file for XTSE\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for EOD data file for XTSE");
            }

            retryCount = 0;

            // Toronto Venture Exchnge - XTSX
            try
            {
                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetEndOfDay(EODDataClient.Exchanges.XTSX, date, out items))
                    {
                        foreach (KeyValuePair<string, OHLC> item in items)
                        {
                            LiquidViewHelper.TryImportPriceByCanadaSymbol(date, item.Key, "XTSX", item.Value.Open, item.Value.High, item.Value.Low, item.Value.Close, item.Value.Close, item.Value.Volume, "LiquidViewSyncTool - EOD Data", out retval);

                            if (retval == 0)
                                Console.WriteLine("Success XTSX {0} {1}", item.Key, item.Value);
                        }
                        retryCount = Program.MAX_RETRIES;
                    }
                    else
                    {
                        Console.WriteLine("Failed to load EOD data file for XTSX attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load EOD data file for XTSX attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for EOD data file for XTSX\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for EOD data file for XTSX");
            }

            retryCount = 0;

            // Opra Options prices
            try
            {

                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetEndOfDay(EODDataClient.Exchanges.Opra, date, out items))
                    {
                        List<string> symbols;
                        if (LiquidViewHelper.TryGetOptionsSymbols(date, "USD", out symbols))
                        {
                            OHLC item;
                            foreach (string symbol in symbols)
                            {
                                if (items.TryGetValue(symbol, out item))
                                {
                                    LiquidViewHelper.TryImportPriceByOptionSymbol(date, symbol, "USD", "XXXX", item.Open, item.High, item.Low, item.Close, item.Close, item.Volume, "LiquidViewSyncTool - EOD Data OPRA", out retval);

                                    if (retval == 0)
                                    {
                                        Console.WriteLine("Success OPRA {0} {1}", symbol, item);
                                    }
                                }
                                else
                                {
                                    LiquidViewHelper.LogException("LiquidViewSyncTool", "Options price not found for {0}", symbol);
                                }
                            }
                            retryCount = Program.MAX_RETRIES;
                        }
                        else
                        {
                            Console.WriteLine("Failed to load Options symbols for US options on OPRA, attempt " + retryCount + " of " + Program.MAX_RETRIES);
                            LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load Options symbols for US options on OPRA, attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Failed to load EOD data file for OPRA attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load EOD data file for OPRA attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }

                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for EOD data file for OPRA\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for EOD data file for OPRA");
            }
        }


        /// <summary>
        /// import splits from EOD Data split files
        /// </summary>
        public static void DoSplits()
        {
            DateTime date;

            if (DateTime.Today.DayOfWeek == DayOfWeek.Sunday)
                date = DateTime.Today.Subtract(new TimeSpan(2, 0, 0, 0));
            if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)
                date = DateTime.Today.Subtract(new TimeSpan(3, 0, 0, 0));
            else
                date = DateTime.Today.Subtract(new TimeSpan(1, 0, 0, 0));

            Dictionary<string, Split> items;
            int retval;

            int retryCount = 0;
            string umtf;

            // LSE
            var lseSymbolMap = (from csvline in Properties.Settings.Default.EODSymbolMap_LSE.Split(',')
                                let csv = csvline.Split(':')
                                where csv.Length == 2
                                select new
                                {
                                    EOD = csv[0],
                                    UMTF = csv[1]
                                }).ToDictionary(a => a.EOD, b => b.UMTF);
            try
            {
                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetSplits(EODDataClient.Exchanges.LSE, out items))
                    {
                        foreach (KeyValuePair<string, Split> item in items)
                        {
                            if (!lseSymbolMap.TryGetValue(item.Key, out umtf))
                                umtf = item.Key;

                            LiquidViewHelper.TryImportSplitByUMTF(umtf + "l", item.Value.Date, item.Value.Recieves, item.Value.From, out retval);

                            if (retval == 0)
                                Console.WriteLine("Success " + umtf + "l " + item.Value.ToString());
                        }
                        retryCount = Program.MAX_RETRIES;
                    }
                    else
                    {
                        Console.WriteLine("Failed to load Split data file for XLON attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load Split data file for XLON attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for Split data file for XLON\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for Split data file for XLON");
            }

            retryCount = 0;


            // NYSE
            try
            {
                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetSplits(EODDataClient.Exchanges.NYSE, out items))
                    {
                        foreach (KeyValuePair<string, Split> item in items)
                        {
                            LiquidViewHelper.TryImportSplitByUSSymbol(item.Key, "XNYS", item.Value.Date, item.Value.Recieves, item.Value.From, out retval);

                            if (retval == 0)
                                Console.WriteLine("Success Nyse {0} {1}", item.Key, item.Value);
                        }
                        retryCount = Program.MAX_RETRIES;
                    }
                    else
                    {
                        Console.WriteLine("Failed to load Split data file for Nyse attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load Split data file for Nyse attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for Split data file for Nyse\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for Split data file for Nyse");
            }

            retryCount = 0;

            // Nasdaq
            try
            {
                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetSplits(EODDataClient.Exchanges.Nasdaq, out items))
                    {
                        foreach (KeyValuePair<string, Split> item in items)
                        {
                            LiquidViewHelper.TryImportSplitByUSSymbol(item.Key, "XNAS", item.Value.Date, item.Value.Recieves, item.Value.From, out retval);

                            if (retval == 0)
                                Console.WriteLine("Success Nasdaq {0} {1}", item.Key, item.Value);
                        }
                        retryCount = Program.MAX_RETRIES;
                    }
                    else
                    {
                        Console.WriteLine("Failed to load Split data file for Nasdaq attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load Split data file for Nasdaq attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for Split data file for Nasdaq\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for Split data file for Nasdaq");
            }

            retryCount = 0;

            // Arca
            try
            {

                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetSplits(EODDataClient.Exchanges.Amex, out items))
                    {
                        foreach (KeyValuePair<string, Split> item in items)
                        {
                            LiquidViewHelper.TryImportSplitByUSSymbol(item.Key, "ARCX", item.Value.Date, item.Value.Recieves, item.Value.From, out retval);

                            if (retval == 0)
                                Console.WriteLine("Success ARCX {0} {1}", item.Key, item.Value);
                        }
                        retryCount = Program.MAX_RETRIES;
                    }
                    else
                    {
                        Console.WriteLine("Failed to load Split data file for ARCX attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load Split data file for ARCX attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for Split data file for ARCX\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for Split data file for ARCX");
            }

            retryCount = 0;

            // Milan
            try
            {
                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetSplits(EODDataClient.Exchanges.LSE, out items))
                    {
                        foreach (KeyValuePair<string, Split> item in items)
                        {
                            if (!lseSymbolMap.TryGetValue(item.Key, out umtf))
                                umtf = item.Key;

                            LiquidViewHelper.TryImportSplitByUMTF(umtf + "m", item.Value.Date, item.Value.Recieves, item.Value.From, out retval);

                            if (retval == 0)
                                Console.WriteLine("Success " + umtf + "m " + item.Value.ToString());
                        }
                        retryCount = Program.MAX_RETRIES;
                    }
                    else
                    {
                        Console.WriteLine("Failed to load Split data file for XMIL attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load Split data file for XMIL attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for Split data file for XMIL\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for Split data file for XMIL");
            }

            retryCount = 0;

            // Toronto Stock Exchnge - XTSE
            try
            {
                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetSplits(EODDataClient.Exchanges.XTSE, out items))
                    {
                        foreach (KeyValuePair<string, Split> item in items)
                        {
                            LiquidViewHelper.TryImportSplitByCanadaSymbol(item.Key, item.Value.Date, item.Value.Recieves, item.Value.From, out retval);

                            if (retval == 0)
                                Console.WriteLine("Success XTSE {0} {1}", item.Key, item.Value);
                        }
                        retryCount = Program.MAX_RETRIES;
                    }
                    else
                    {
                        Console.WriteLine("Failed to load Split data file for XTSE attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load Split data file for XTSE attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for Split data file for XTSE\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for Split data file for XTSE");
            }

            retryCount = 0;

            // Toronto Venture Exchnge - XTSX
            try
            {
                while (retryCount++ < Program.MAX_RETRIES)
                {
                    if (_eodDataClient.TryGetSplits(EODDataClient.Exchanges.XTSX, out items))
                    {
                        foreach (KeyValuePair<string, Split> item in items)
                        {
                            LiquidViewHelper.TryImportSplitByCanadaSymbol(item.Key, item.Value.Date, item.Value.Recieves, item.Value.From, out retval);

                            if (retval == 0)
                                Console.WriteLine("Success XTSX {0} {1}", item.Key, item.Value);
                        }
                        retryCount = Program.MAX_RETRIES;
                    }
                    else
                    {
                        Console.WriteLine("Failed to load Split data file for XTSX attempt " + retryCount + " of " + Program.MAX_RETRIES);
                        LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to load Split data file for XTSX attempt " + retryCount + " of " + Program.MAX_RETRIES);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to process data for Split data file for XTSX\n" + e);
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Failed to process data for Split data file for XTSX");
            }

            retryCount = 0;


        }


        /// <summary>
        /// Import US names for stocks and options from EOD data ftp files
        /// </summary>
        public static void DoUS()
        {
            int retVal;
            int? stockId;
            Dictionary<string, string> items;

            Console.WriteLine("Processing NYSE Names");
            // NYSE Names
            if (_eodDataClient.TryGetNames(EODDataClient.Exchanges.NYSE, out items))
            {
                foreach (KeyValuePair<string, string> stock in items)
                {
                    retVal = LiquidViewHelper.UpdateInstrumentStockUSCanada("XNYS", "USD", stock.Key, stock.Value, 2, out stockId);

                    if (retVal < 0)
                    {
                        Console.WriteLine("Failed (" + retVal + ") to write XNYS " + stock.Key + " " + stock.Value);
                    }
                    else if (retVal == 0)
                    {
                        Console.WriteLine("New NYSE stock " + stock.Key + " created");
                        Program.DBLinq.ExceptionHandlerThrow("LiquidViewSyncTool", "New NYSE stock " + stock.Key + " created", null);
                    }
                }
            }
            else
            {
                Console.WriteLine("Failed NYSE Names");
            }

            Console.WriteLine("Processing Nasdaq Names");
            // NASDAQ Names
            if (_eodDataClient.TryGetNames(EODDataClient.Exchanges.Nasdaq, out items))
            {
                foreach (KeyValuePair<string, string> stock in items)
                {
                    retVal = LiquidViewHelper.UpdateInstrumentStockUSCanada("XNAS", "USD", stock.Key, stock.Value, 2, out stockId);
                    if (retVal < 0)
                    {
                        Console.WriteLine("Failed (" + retVal + ") to write XNAS " + stock.Key + " " + stock.Value);
                    }
                    else if (retVal == 0)
                    {
                        Console.WriteLine("New Nasdaq stock " + stock.Key + " created");
                        Program.DBLinq.ExceptionHandlerThrow("LiquidViewSyncTool", "New Nasdaq stock " + stock.Key + " created", null);
                    }
                }
            }
            else
            {
                Console.WriteLine("Failed Nasdaq Names");
            }

            Console.WriteLine("Processing ARCX Names");
            // ARCX Names
            if (_eodDataClient.TryGetNames(EODDataClient.Exchanges.Amex, out items))
            {
                foreach (KeyValuePair<string, string> stock in items)
                {
                    retVal = LiquidViewHelper.UpdateInstrumentStockUSCanada("ARCX", "USD", stock.Key, stock.Value, 2, out stockId);
                    if (retVal < 0)
                    {
                        Console.WriteLine("Failed (" + retVal + ") to write ARCX (Amex) " + stock.Key + " " + stock.Value);
                    }
                    else if (retVal == 0)
                    {
                        Console.WriteLine("New ARCX stock " + stock.Key + " created");
                        Program.DBLinq.ExceptionHandlerThrow("LiquidViewSyncTool", "New ARCX (AMEX) stock " + stock.Key + " created", null);
                    }
                }
            }
            else
            {
                Console.WriteLine("Failed ARCX Names");
            }

        }



        /// <summary>
        /// Import Candian names for stock from EOD Data ftp files
        /// </summary>
        public static void DoCanada()
        {
            int retVal;
            int? stockId;
            Dictionary<string, string> items;

            Console.WriteLine("Processing XTSE Names");
            // XTSE
            if (_eodDataClient.TryGetNames(EODDataClient.Exchanges.XTSE, out items))
            {
                foreach (KeyValuePair<string, string> stock in items)
                {
                    retVal = LiquidViewHelper.UpdateInstrumentStockUSCanada("XTSE", "CAD", stock.Key, stock.Value, 2, out stockId);

                    if (retVal < 0)
                    {
                        Console.WriteLine("Failed (" + retVal + ") to write XTSE " + stock.Key + " " + stock.Value);
                    }
                    else if (retVal == 0)
                    {
                        Console.WriteLine("New XTSE stock " + stock.Key + " created");
                        Program.DBLinq.ExceptionHandlerThrow("LiquidViewSyncTool", "New XTSE stock " + stock.Key + " created", null);
                    }
                }
            }
            else
            {
                Console.WriteLine("Failed XTSE Names");
            }

            Console.WriteLine("Processing XTSX Venture Names");
            // XTSX - Venture
            if (_eodDataClient.TryGetNames(EODDataClient.Exchanges.XTSX, out items))
            {
                foreach (KeyValuePair<string, string> stock in items)
                {
                    retVal = LiquidViewHelper.UpdateInstrumentStockUSCanada("XTSX", "CAD", stock.Key, stock.Value, 2, out stockId);
                    if (retVal < 0)
                    {
                        Console.WriteLine("Failed (" + retVal + ") to write XTSX " + stock.Key + " " + stock.Value);
                    }
                    else if (retVal == 0)
                    {
                        Console.WriteLine("New XTSX Venture stock " + stock.Key + " created");
                        Program.DBLinq.ExceptionHandlerThrow("LiquidViewSyncTool", "New XTSX Venture stock " + stock.Key + " created", null);
                    }
                }
            }
            else
            {
                Console.WriteLine("Failed XTSX Venture Names");
            }

        }

    }
}
